//
//  API.m
//  Alayicai
//
//  Created by ibokan on 15/7/3.
//  Copyright (c) 2015年 FiM. All rights reserved.
//

#import "API.h"
#import "AFNetworking.h"
//#import "PlayWithMe.h"
//#define HOST @"http://192.168.18.47/shopzfaaaa/index.php?act=ueserapp&op="
@interface API()
@property(retain,nonatomic)NSDictionary *returnData;

@end


@implementation API

static API *obj=nil;

+(id)getObject{
    @synchronized(self){
        if (!obj) {
            obj=[API new];
        }
        return obj;
    }
}

+(instancetype)allocWithZone:(struct _NSZone *)zone{
    if (!obj) {
        obj=[super allocWithZone:zone];
    }
    return obj;
}

#pragma mark AFNetworking框架
//创建管理器
+(AFHTTPRequestOperationManager*) getManager{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    return manager;
    
}
+(AFHTTPRequestOperationManager*) getManager1{
    AFHTTPRequestOperationManager * manager1 = [AFHTTPRequestOperationManager manager];
    manager1.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager1.responseSerializer = [AFHTTPResponseSerializer serializer];
    return manager1;
    
}
//通用网络访问方法
+(void)netAccess:(NSString *)url andMethor:(NSString *)methor andDict:(NSDictionary *)dict success:(void (^)(NSDictionary *))mysuccess falure:(void (^)(NSError *))myfalure{

    AFHTTPRequestOperationManager *ma=[API getManager];
    
    if ([methor isEqualToString:@"GET"]) {

        [ma GET:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {

            NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
            mysuccess(resDict);
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            myfalure(error);
        }];
    }else if ([methor isEqualToString:@"POST"]) {
        [ma POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {

            NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
            mysuccess(resDict);
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            myfalure(error);
        }];
    }
}
//通用网络访问方法
+(void)netAccess1:(NSString *)url andMethor:(NSString *)methor andDict:(NSDictionary *)dict success:(void (^)(NSDictionary *))mysuccess falure:(void (^)(NSError *))myfalure{
    //获取管理器对象
    AFHTTPRequestOperationManager *ma=[API getManager1];
    
    if ([methor isEqualToString:@"GET"]) {

        [ma GET:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {

            NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
            mysuccess(resDict);
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            myfalure(error);
        }];
    }else if ([methor isEqualToString:@"POST"]) {
        [ma POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {

            NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
            mysuccess(resDict);
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            myfalure(error);
        }];
    }
}

//上传图片
+(void)netImage:(NSString *)url andData:(NSData *)data imageName:(NSString *)imageKey success:(void (^)(NSDictionary *))mysuccess falure:(void (^)(NSError *))myfalure{

    AFHTTPRequestOperationManager *ma=[API getManager];
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    NSString *key = [[NSUserDefaults standardUserDefaults]objectForKey:@"key"];
    [param setObject:key forKey:@"key"];
    
    [ma POST:url parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
 
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
        
        // 上传图片，以文件流的格式
        [formData appendPartWithFileData:data name:imageKey fileName:fileName mimeType:@"image/jpeg"];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //将JSON解析成字典
        NSDictionary*resDict = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        mysuccess(resDict);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        myfalure(error);
        
    }];
    
}
+(void)requestAppendKey:(NSString *)url params:(NSDictionary *)params success:(void(^)(NSDictionary *successCode)) mysuccess falure:(void(^)(NSError * er)) myfalure
{
    NSMutableDictionary *dict = nil;
    if (params) {
        dict = [NSMutableDictionary dictionaryWithDictionary:params];
    }else
    {
        dict = [NSMutableDictionary dictionary];
    }
    
    NSString *key = [[NSUserDefaults standardUserDefaults] objectForKey:@"key"];
    [dict setObject:key forKey:@"key"];
    [self netAccess:url andMethor:@"GET" andDict:dict success:mysuccess falure:myfalure];
}
+(void)requestPostAppendKey:(NSString *)url params:(NSDictionary *)params success:(void(^)(NSDictionary *successCode)) mysuccess falure:(void(^)(NSError * er)) myfalure
{
    NSMutableDictionary *dict = nil;
    if (params) {
        dict = [NSMutableDictionary dictionaryWithDictionary:params];
    }else
    {
        dict = [NSMutableDictionary dictionary];
    }
    
    NSString *key = [[NSUserDefaults standardUserDefaults] objectForKey:@"key"];
    [dict setObject:key forKey:@"key"];
    [self netAccess:url andMethor:@"POST" andDict:dict success:mysuccess falure:myfalure];
}
//停止一切请求
+(void)stopRequester
{
    //获取管理器对象
    AFHTTPRequestOperationManager *ma=[API getManager];
    [ma.operationQueue cancelAllOperations];
}

@end
